﻿/*
 * Created by SharpDevelop.
 * User: Iveely Liu
 * Date: 2013/4/4
 * Time: 20:40
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Management;
using System.Net;
using System.Text;

namespace IveelySE.Dream.Common
{
    /// <summary>
    /// Description of SystemInfo.
    /// </summary>
    public class SystemDiagnostics
    {
        /// <summary>
        /// 获取系统所有信息
        /// </summary>
        /// <returns></returns>
        public static string GetInformation()
        {
            string info = "NULL";//string.Format("Machine Name:{0};{1};{2}", GetMachineName(), GetSofewareInfo(), GetHardwareInfo());
            return info;
        }

        /// <summary>
        /// 获取硬件信息
        /// </summary>
        /// <returns>返回当前机器的硬件信息</returns>
        private static string GetHardwareInfo()
        {
            string information = string.Format("Hardware:CPU[{0}%],Memery:{1}Mb,Disk:{2}Mb", GetCPUInfo(), GetMemeryInfo(), GetDiskInfo());
            return information;
        }

        /// <summary>
        /// 获取软件信息
        /// </summary>
        /// <returns>返回信息集合</returns>
        private static string GetSofewareInfo()
        {
            string information = string.Format("Software:{0}", GetProcessInfo().ToString());
            return information;
        }

        /// <summary>
        /// 获取CPU使用频率
        /// </summary>
        /// <returns>返回当前CPU的占有率</returns>
        private static double GetCPUInfo()
        {
            PerformanceCounter cpuCounter = new PerformanceCounter();
            cpuCounter.CategoryName = "Processor";
            cpuCounter.CounterName = "% Processor Time";
            cpuCounter.InstanceName = "_Total";
            return cpuCounter.NextValue();
        }

        /// <summary>
        /// 获取内存使用情况
        /// </summary>
        /// <returns>返回剩余内存大小(单位：MB)</returns>
        private static double GetMemeryInfo()
        {
            PerformanceCounter ramCounter = new PerformanceCounter("Memory", "Available MBytes");
            return ramCounter.NextValue();
        }

        /// <summary>
        /// 获取硬盘使用情况
        /// </summary>
        /// <returns>返回D盘剩余空间大小(单位：MB)</returns>
        private static double GetDiskInfo()
        {
            ManagementObject obj = new ManagementObject("win32_logicaldisk.deviceid=\"D:\"");
            double size = ((((double)Convert.ToInt64(obj["FreeSpace"].ToString())) / 1024) / 1024) / 1024;
            return size;
        }

        /// <summary>
        /// 获取机器的所有进程
        /// </summary>
        /// <returns>返回进程信息</returns>
        private static StringBuilder GetProcessInfo()
        {
            Process[] processes = Process.GetProcesses();
            StringBuilder builder = new StringBuilder();
            foreach (Process process in processes)
            {
                string infor = string.Format("Process Name:{0},Memory-{1}Mb", process.ProcessName, process.WorkingSet64 / 1024 / 1024 / 1024);
                builder.AppendLine(infor);
            }
            return builder;
        }

        /// <summary>
        /// 获取机器名称
        /// </summary>
        /// <returns>返回机器名</returns>
        private static string GetMachineName()
        {
            return Dns.GetHostName();
        }
    }
}
